package zilvic.resources.sprites;

import java.awt.Graphics;
import org.duncan.Math2D.Point2D;
import zilvic.game.room.entity.actor.Actor;
import zilvic.resources.ByteMagic;
import zilvic.resources.Parsable;

/**
 *
 * @author Duncan
 */
public class ActorSprite implements Drawable, Parsable {
    private Sequence[] dir = new Sequence[4];

    public ActorSprite() {
        
    }

    public ActorSprite(Sequence up, Sequence left, Sequence down, Sequence right) {
        dir[Actor.DIRECTION_UP]    = up;
        dir[Actor.DIRECTION_LEFT]  = left;
        dir[Actor.DIRECTION_DOWN]  = down;
        dir[Actor.DIRECTION_RIGHT] = right;
    }

    public ActorSprite setSequence(byte direction, Sequence sequence) {
        dir[direction] = sequence;
        return this;
    }

    public Sequence getSequence(byte direction) {
        return dir[direction];
    }

    /**
     * * Draw the sprite onto the graphics context provided
     *
     * @param g The graphics context on which to draw the sprite
     * @param position The location at which to draw the sprite
     * @param step The step of the animation. Can be either 0, 1, 2, or 3.
     * @param direction The direction of the sprite.
     * @See Actor
     * @return A reference to this.
     */
    public ActorSprite draw(Graphics g, Point2D position, byte step, byte direction) {
        getSequence(direction).draw(g, position, step, direction);
        return this;
    }

    /**
     * The format is as follows.
     * 0: The sequences.
     * @return This effect in bytes.
     */
    public byte[] toBytes() {
        return ByteMagic.parsablesToBytes(dir);
    }

    public void fromBytes(byte[] bytes) {
        dir = (Sequence[]) ByteMagic.bytesToParsables(bytes);
    }
}